package com.yt.website.action;

import com.yt.core.MsgBean;
import com.yt.util.MD5Util;
import com.yt.website.action.base.BaseAction;
import com.yt.website.api.interfaces.UserInfoService;
import com.yt.website.api.model.UserInfo;
import com.yt.website.exception.DataException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/")
public class RegisterAction extends BaseAction{

    @Autowired
    private UserInfoService userInfoService;

    @RequestMapping("/toRegister")
    public ModelAndView toRegister() {
        ModelAndView mav = new ModelAndView("register");
        return mav;
    }
    @RequestMapping("/toRegisterCompany")
    public ModelAndView toRegisterCompany() {
        ModelAndView mav = new ModelAndView("registerCompany");
        return mav;
    }

    @RequestMapping("/register")
    public
    @ResponseBody
    Object register(@RequestParam(value="regType", required = true) Integer regType,
                    @RequestParam(value="code", required = true) String code,
                    @RequestParam(value="mobile", required = true) String mobile,
                    @RequestParam(value="mobileCode", required = true) String mobileCode,
                    @RequestParam(value="passwd", required = true) String passwd,
                    @RequestParam(value = "address", required = false) String address,
                    @RequestParam(value = "nick", required = false) String nick,
                    /*** For Personal Register ***/
                    @RequestParam(value = "realName", required = false) String realName,
                    /*** For Company Register ***/
                    @RequestParam(value = "cName", required = false) String cName,
                    @RequestParam(value = "cContactName", required = false) String cContactName,
                    @RequestParam(value = "cPhone", required = false) String cPhone,
                    HttpServletRequest request) throws Exception{

        if (!CodeAction.checkCode(code, request)){
            throw new DataException("验证码不正确!");
        }
        if (!MobileCodeAction.checkCode(mobileCode, request)){
            throw new DataException("手机验证码不正确!");
        }
        if(!userInfoService.checkMobile(mobile)){
            throw new DataException("手机号已存在");
        }

        if(regType == 2){
            return companyRegister(cName, cContactName, cPhone, mobile, nick, passwd, address);
        }else{
            return personalRegister(realName, mobile, nick, passwd, address);
        }
    }

    @RequestMapping("/checkMobile")
    public
    @ResponseBody
    Object send(@RequestParam(value="mobile", required = true) String mobile,
                HttpServletRequest request) throws Exception{
        MsgBean msg = new MsgBean();
        if(!userInfoService.checkMobile(mobile)){
           msg.setFailure("手机号已经存在！");
        }
        return msg.returnMsg();
    }

    private MsgBean personalRegister(String realName, String mobile, String nick, String passwd, String address){
        MsgBean msg = new MsgBean();
        UserInfo userInfo = new UserInfo();
        userInfo.setMobile(mobile);
        userInfo.setRealName(realName);
        userInfo.setNick(nick);
        userInfo.setPassword(MD5Util.MD5(passwd));
        userInfo.setAddress(address);
        userInfo.setUserType((short)1);
        userInfoService.insert(userInfo);

        return msg;
    }

    private MsgBean companyRegister(String cName, String cContactName, String cPhone, String mobile, String nick, String passwd, String address){
        MsgBean msg = new MsgBean();
        UserInfo userInfo = new UserInfo();
        userInfo.setMobile(mobile);
        userInfo.setRealName(cContactName);
        userInfo.setCompany(cName);
        userInfo.setPhone(cPhone);
        userInfo.setNick(nick);
        userInfo.setPassword(MD5Util.MD5(passwd));
        userInfo.setAddress(address);
        userInfo.setUserType((short)2);
        userInfoService.insert(userInfo);

        return msg;
    }
}
